package com.bianguo.android.iocdemo.view.widget;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.RectF;
import android.graphics.Region;
import android.media.Ringtone;
import android.support.annotation.Nullable;
import android.support.v4.graphics.PathParser;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;

import com.bianguo.android.eventbuslibrary.utils.MLog;
import com.bianguo.android.iocdemo.R;

public class RotateView extends View {

    private Paint mPaint;
    private float mCenterX, mCenterY;
    private float mRadius;
    private Path mPath;
    private float[] pos, tan;
    private float currentValue;
    private Matrix mMatrix;
    private Bitmap mBitmap;
    private PathMeasure pathMeasure;

    public RotateView(Context context) {
        this(context, null);
    }

    public RotateView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RotateView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.GRAY);
        mPaint.setStrokeWidth(2);
        mPaint.setAntiAlias(true);

        mRadius = 100;

        mPath = new Path();
        pathMeasure = new PathMeasure();
        mMatrix = new Matrix();
        pos = new float[2];
        tan = new float[2];
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 2;       // 缩放图片
        mBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.plane, options);
        ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
        animator.setInterpolator(new AccelerateDecelerateInterpolator());
        animator.setRepeatCount(5);
        animator.setDuration(2000);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                currentValue = (float) valueAnimator.getAnimatedValue();
                invalidate();
            }
        });
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                animation.cancel();
                animation.removeAllListeners();
            }
        });
        animator.start();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(getMeasuredDimension(widthMeasureSpec), getMeasuredDimension(heightMeasureSpec));
    }

    private int getMeasuredDimension(int measureSpec) {
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);
        MLog.i("size = " + size);
        switch (mode) {
            case MeasureSpec.EXACTLY:
                break;
            case MeasureSpec.AT_MOST:
                size = (int) (2 * mRadius);
                break;
        }
        return size;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mCenterX = w / 2;
        mCenterY = h / 2;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mPath.addCircle(mCenterX, mCenterY, mRadius, Path.Direction.CW);
        pathMeasure.setPath(mPath, false);
        pathMeasure.getPosTan(pathMeasure.getLength() * currentValue, pos, tan);
        mMatrix.reset();
        float degrees = (float) (Math.atan2(tan[1], tan[0]) * 180 / Math.PI);
        int width = mBitmap.getWidth();
        int height = mBitmap.getHeight();
        mMatrix.postRotate(degrees, width / 2, height / 2);
        mMatrix.postTranslate(pos[0] - width / 2, pos[1] - height / 2);
//        canvas.drawPath(mPath, mPaint);
        canvas.drawBitmap(mBitmap, mMatrix, mPaint);
    }
}
